This project leverages the CoinGecko API and the power of Python to create an informative and visually appealing Bitcoin Candlestick Chart spanning the last 30 days. A Candlestick Chart is a widely used financial charting tool that provides valuable insights into the price movements of cryptocurrencies, including Bitcoin.
!pip install pycoingecko
from pycoingecko import CoinGeckoAPI
import pandas as pd
Defaulting to user installation because normal site-packages is not writeable Requirement already satisfied: pycoingecko in c:\users\dinor\appdata\roaming\python\python311\site-packages (3.1.0) Requirement already satisfied: requests in c:\programdata\anaconda3\lib\site-packages (from pycoingecko) (2.31.0) Requirement already satisfied: charset-normalizer<4,>=2 in c:\programdata\anaconda3\lib\site-packages (from requests->pycoingecko) (2.0.4) Requirement already satisfied: idna<4,>=2.5 in c:\programdata\anaconda3\lib\site-packages (from requests->pycoingecko) (3.4) Requirement already satisfied: urllib3<3,>=1.21.1 in c:\programdata\anaconda3\lib\site-packages (from requests->pycoingecko) (1.26.16) Requirement already satisfied: certifi>=2017.4.17 in c:\programdata\anaconda3\lib\site-packages (from requests->pycoingecko) (2023.7.22)
from datetime import datetime
import time
def unix_time(year,month,day,hour,second):
date_time = datetime.datetime(year,month,day,hour,second)
return time.mktime(date_time.timetuple())
def human_time(unix_time):
return datetime.datetime.fromtimesstamp(unix_time)
cg = CoinGeckoAPI()
bitcoin_data = cg.get_coin_market_chart_by_id(
id="bitcoin",
vs_currency = "usd",
days = 30
)
print(f'keys {bitcoin_data.keys()}')
keys dict_keys(['prices', 'market_caps', 'total_volumes'])
First we select only the first key (prices) wich contains a TimeStamp (number of seconds since) and the respective price. We transform the data into a dataframe so we can manipulate more easily.
bitcoin_data_price = bitcoin_data['prices']
data = pd.DataFrame(bitcoin_data_price, columns=['TimeStamp','Price'])
data
| TimeStamp | Price | |
|---|---|---|
| 0 | 1695574805796 | 26570.733979 |
| 1 | 1695578469402 | 26657.753583 |
| 2 | 1695582049432 | 26600.275122 |
| 3 | 1695585670396 | 26447.459302 |
| 4 | 1695589238846 | 26505.534806 |
| ... | ... | ... |
| 717 | 1698152477689 | 34606.406639 |
| 718 | 1698156033985 | 34271.579650 |
| 719 | 1698159608858 | 34443.238871 |
| 720 | 1698163262722 | 33689.133899 |
| 721 | 1698166609000 | 34074.789662 |
722 rows × 2 columns
data['Date'] = pd.to_datetime(data['TimeStamp'], unit='ms')
data
| TimeStamp | Price | Date | |
|---|---|---|---|
| 0 | 1695574805796 | 26570.733979 | 2023-09-24 17:00:05.796 |
| 1 | 1695578469402 | 26657.753583 | 2023-09-24 18:01:09.402 |
| 2 | 1695582049432 | 26600.275122 | 2023-09-24 19:00:49.432 |
| 3 | 1695585670396 | 26447.459302 | 2023-09-24 20:01:10.396 |
| 4 | 1695589238846 | 26505.534806 | 2023-09-24 21:00:38.846 |
| ... | ... | ... | ... |
| 717 | 1698152477689 | 34606.406639 | 2023-10-24 13:01:17.689 |
| 718 | 1698156033985 | 34271.579650 | 2023-10-24 14:00:33.985 |
| 719 | 1698159608858 | 34443.238871 | 2023-10-24 15:00:08.858 |
| 720 | 1698163262722 | 33689.133899 | 2023-10-24 16:01:02.722 |
| 721 | 1698166609000 | 34074.789662 | 2023-10-24 16:56:49.000 |
722 rows × 3 columns
we gonna need to group the data by the min, max, first and last price for each date
candlestick_data = data.groupby(data.Date.dt.date).agg({'Price':['min','max', 'first','last']})
candlestick_data.head()
| Price | ||||
|---|---|---|---|---|
| min | max | first | last | |
| Date | ||||
| 2023-09-24 | 26447.459302 | 26657.753583 | 26570.733979 | 26503.235603 |
| 2023-09-25 | 26059.043075 | 26357.550153 | 26257.583770 | 26287.076367 |
| 2023-09-26 | 26111.574712 | 26347.306498 | 26296.922317 | 26151.495532 |
| 2023-09-27 | 26166.382102 | 26805.628892 | 26204.757591 | 26288.209341 |
| 2023-09-28 | 26344.993094 | 27146.515346 | 26350.146895 | 27013.058110 |
import plotly.graph_objects as go
fig = go.Figure(data=[go.Candlestick(x = candlestick_data.index,
open=candlestick_data['Price']['first'],
high = candlestick_data['Price']['max'],
low = candlestick_data['Price']['min'],
close = candlestick_data['Price']['last'])
])
fig.update_layout(xaxis_rangeslider_visible = False, xaxis_title = 'Date',
yaxis_title='Price (USD $)', title = 'Bitcoin Candlestick Chart Over Past 30 Days')
fig.show()